package com.prs.crm.dao.crm.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Property;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.prs.crm.dao.crm.LinkmanDao;
import com.prs.crm.dao.impl.BaseDaoImpl;
import com.prs.crm.domain.crm.Customer;
import com.prs.crm.domain.crm.CustomerAgent;
import com.prs.crm.domain.crm.CustomerSupplier;
import com.prs.crm.domain.crm.Linkman;

public class LinkmanDaoImpl extends BaseDaoImpl<Linkman> implements LinkmanDao{

	@SuppressWarnings("unchecked")
	@Override
	public List<Linkman> getLinkmanByCustomer(Customer customer) {
		// TODO Auto-generated method stub
		Criteria criteria = this.getSession().createCriteria(Linkman.class);
		criteria.add(Property.forName("customer").eq(customer));
		return criteria.list();
	}

	@Override
	public Collection<Linkman> getAllLinkman() {
		// TODO Auto-generated method stub
		return (Collection<Linkman>) this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session arg0)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				Collection<Linkman> results = new ArrayList<Linkman>();
				Criteria criteria = arg0.createCriteria(Linkman.class);

				return criteria.list();
			}
			
		});
	}

	@Override
	public List<Linkman> getLinkmanByCustomerAgent(CustomerAgent customerAgent) {
		// TODO Auto-generated method stub
		Criteria criteria = this.getSession().createCriteria(Linkman.class);
		criteria.add(Property.forName("customerAgent").eq(customerAgent));
		return criteria.list();
	}

	@Override
	public List<Linkman> getLinkmanByCustomerSupplier(
			CustomerSupplier customerSupplier) {
		Criteria criteria = this.getSession().createCriteria(Linkman.class);
		criteria.add(Property.forName("customerSupplier").eq(customerSupplier));
		return criteria.list();
	}

}
